package com.aes.test;

import java.io.File;

import com.ace.R;
import com.aes.core.AesCallback;
import com.aes.core.AesHelper;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * AES加解密Demo
 * @author zhouzhineng
 *
 */
public class AesDemoActivity extends Activity implements OnClickListener{

	private Button mBtnEncode;
	private Button mBtnDecode;
	private AesHelper mAesHelper;
	
	private final String encrypt_str = "Hello,I am a String for encrypt!";
	private String decrypt_str = "";
	
	private final String key = "abcd123";
	
	private ProgressDialog mProgressDlg;
	
	private final int ENCRYPT_SUCC = 0x1;		//加密成功
	private final int ENCRYPT_FAIL = 0x1 << 1;	//加密失败
	private final int DECRYPT_SUCC = 0x1 << 2;	//解密成功
	private final int DECRYPT_FAIL = 0x1 << 3;  //解密失败
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.demo_layout);
	
		init();
	}
	
	private void init() {
		createTestDir();
		mBtnEncode = (Button)findViewById(R.id.BtnEncode);
		mBtnDecode = (Button)findViewById(R.id.BtnDecode);
		mBtnEncode.setOnClickListener(this);
		mBtnDecode.setOnClickListener(this);
		mAesHelper = AesHelper.getInstance(this);
		mAesHelper.SetAesSeed(key);
		AesCallback aesCallback = new AesCallback(){


			@Override
			public void preEncrypt() {
				// TODO Auto-generated method stub
				showProgressDlg(true,"开始加密");
			}

			@Override
			public void preDecrypt() {
				// TODO Auto-generated method stub
				showProgressDlg(true,"开始解密");
			}

			@Override
			public void onEncrypt(boolean result) {
				// TODO Auto-generated method stub
				showProgressDlg(false,"");
				Message msg = new Message();
				msg.what = ENCRYPT_SUCC;
				MsgHandler.sendMessage(msg);
			}

			@Override
			public void onDecrypt(boolean result) {
				// TODO Auto-generated method stub
				showProgressDlg(false,"");
				Message msg = new Message();
				msg.what = DECRYPT_SUCC;
				MsgHandler.sendMessage(msg);
			}
			
		};
		mAesHelper.setAesCallback(aesCallback);
	}
	
	private Handler MsgHandler = new Handler()  {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			String statusMsg = "";
			switch(msg.what) {
			case ENCRYPT_SUCC:
				statusMsg = "加密成功!";
				break;
			case ENCRYPT_FAIL:
				statusMsg = "加密失败!";
				showProgressDlg(false, "");
				break;
			case DECRYPT_SUCC:
				statusMsg = "解密成功!";
				break;
			case DECRYPT_FAIL:
				statusMsg = "解密失败!";
				showProgressDlg(false, "");
				break;
			}
			if(msg.obj != null) {
				statusMsg += "=====>:" + (String)msg.obj;
			}
			Toast.makeText(AesDemoActivity.this, statusMsg, Toast.LENGTH_SHORT).show();
		}
		
		
	};
	
	//建立测试目录，如使用该目录进行测试则需要往里面添加测试文件
	private void createTestDir() {
		File sdcardDir =Environment.getExternalStorageDirectory();
        String path=sdcardDir.getPath()+"/aes_test";
        File folder = new File(path);
        if (!folder.exists()) folder.mkdirs();
	}
	
	private void showProgressDlg(boolean isShow,String msg) {
		if(isShow) {
			if(mProgressDlg == null) {
				mProgressDlg = new ProgressDialog(this);
			}
			mProgressDlg.setMessage(msg);
			mProgressDlg.show();
		} else {
			if(mProgressDlg != null) {
				mProgressDlg.dismiss();
			}
		}
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()) {
		case R.id.BtnEncode:			//加密
			try{
				//mAesHelper.encrypt(encrypt_str.getBytes());
				File sdcardDir =Environment.getExternalStorageDirectory();
		        String path=sdcardDir.getPath()+"/aes_test";
				//mAesHelper.encryptFile(path);
		        mAesHelper.handle(path, true);
			}catch(Exception e){
				e.printStackTrace();
			}
			break;
		case R.id.BtnDecode:			//解密
			try {
				//mAesHelper.decrypt(decrypt_str.getBytes());
				File sdcardDir =Environment.getExternalStorageDirectory();
		        String path=sdcardDir.getPath()+"/aes_test";
				//mAesHelper.decryptFile(path);
				mAesHelper.handle(path, false);
			}catch(Exception e) {
				e.printStackTrace();
			}

			break;
		}
	}

	
}
